package com.icefish.daoImpl;

import com.icefish.bean.Comment;
import com.icefish.dao.CommentDao;

import com.icefish.utils.DateTransform;
import com.icefish.utils.DatabaseConnFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.*;
import java.util.ArrayList;

/**
 * Description:
 * Date: 2022/12/4 15:42
 * Author: 26480
 *
 * @since JDK 1.8
 */
public class CommentDaoImpl implements CommentDao {

    // 添加评论信息
    @Override
    public Boolean addComment(Comment comment) throws SQLException {
        System.out.println("+++++++++++++++++++++++++++");
        System.out.println(comment.toString());
        System.out.println("+++++++++++++++++++++++++++");

        String sql = "INSERT  INTO  comment (says_time, says, goods_id, user_id, username,order_id) VALUES (?,?,?,?,?,?)";
        Connection conn = DatabaseConnFactory.getConn();

        // 调用工具类，utilDate转sqlDate
        java.sql.Date sqlDate = DateTransform.util_To_Sql(comment.getSays_time());

        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setDate(1,sqlDate);
        pstmt.setString(2,comment.getSays());
        pstmt.setInt(3,comment.getGoods_id());
        pstmt.setInt(4,comment.getUser_id());
        pstmt.setString(5,comment.getUsername());
        pstmt.setInt(6,comment.getOrder_id());

        //执行DML语句，返回受影响的记录条数
        Integer isok = pstmt.executeUpdate();

        // 关闭连接，释放资源
        if (pstmt != null) {
            pstmt.close();
        }
        if (conn != null) {
            conn.close();
        }

        return (isok == 1);
    }

    @Override
    public ArrayList <Comment> selectCommentByGoodId(Integer id) throws Exception {

        String sql = "select * from comment where goods_id=?";
        Connection conn = DatabaseConnFactory.getConn();

        PreparedStatement psmt = conn.prepareStatement(sql);
        psmt.setInt(1, id);

        ResultSet rst = psmt.executeQuery();

        ArrayList <Comment> comments = new ArrayList <Comment>();
        while (rst.next()) {

            Comment comment = new Comment();
            //获取当前行的每列数据
            comment.setSays_id(rst.getInt("says_id"));
            // 转成正常日期类型 ps（mysql返回时间戳，日期转换工具类需要改写，让前端自己处理吧）
//            comment.setSays_time(DateTransform.sql_To_Util(rst.getDate("says_time")));
            comment.setSays_time(rst.getDate("says_time"));
            comment.setSays(rst.getString("says"));
            comment.setUser_id(rst.getInt("user_id"));
            comment.setUsername(rst.getString("username"));
            comment.setOrder_id(rst.getInt("order_id"));

            comments.add(comment);

        }

        // 关闭连接，释放资源
        if (rst != null) {
            rst.close();
        }
        if (psmt != null) {
            psmt.close();
        }
        if (conn != null) {
            conn.close();
        }

        return comments;
    }

    @Override
    // 根据order_id获取评论信息
    public boolean selectHasCommentByOrderId(Integer order_id) throws Exception {

        String sql = "select * from comment where order_id = ?";
        Connection conn = DatabaseConnFactory.getConn();

        PreparedStatement psmt = conn.prepareStatement(sql);
        psmt.setInt(1, order_id);

        ResultSet rst = psmt.executeQuery();

        boolean has = false;
        while (rst.next()) {
            has = true;
        }

        // 关闭连接，释放资源
        if (rst != null) {
            rst.close();
        }
        if (psmt != null) {
            psmt.close();
        }
        if (conn != null) {
            conn.close();
        }

        return has;
    }

    @Override
    // 根据根据order_id 修改 评论信息
    public boolean updateSaysByOrderId(Integer order_id, String says) throws Exception {
        String sql = "UPDATE comment SET says = ? WHERE order_id = ?";
        Connection conn = DatabaseConnFactory.getConn();

        PreparedStatement pstmt = conn.prepareStatement(sql);

        pstmt.setString(1,says);
        pstmt.setInt(2,order_id);

        //执行DML语句，返回受影响的记录条数
        Integer isok = pstmt.executeUpdate();

        // 关闭连接，释放资源
        if (pstmt != null) {
            pstmt.close();
        }
        if (conn != null) {
            conn.close();
        }

        return (isok == 1);
    }
}
